package Movie;

use Moose;
use Readonly;
use feature qw(switch);

use Price;
# use Regularprice;
# use Childrenprice;
# use Newreleaseprice;

# has 'CHILDRENS' => (
#   is => 'ro',
#   isa => int,
#   default => 2,
# );

# has 'REGULAR' => (
#   is => 'ro',
#   isa => int,
#   default => 0,
# );

# has 'NEW_RELEASE' => (
#   is => 'ro',
#   isa => int,
#   default => 1,
# );

Readonly::Scalar our $CHILDREN => 2;
Readonly::Scalar our $REGULAR => 0;
Readonly::Scalar our $NEW_RELEASE => 1;

has '_title' => (
    isa => 'Str',
    is => 'ro',
    reader => 'get_title',
    required => 1,
);

# has '_price_code' => (
#     isa => 'Int',
#     is => 'bare',
# );

has '_price' => (
    isa => 'Price',
    # builder => '_set_price',
    writer => 'set_price',
    reader => 'get_price',
    # lazy => 1,
);

around BUILDARGS => sub {
    my $orig  = shift;
    my $class = shift;
    my $price_code = 0;
    my $skip_next = 0;
    my @new_args = ();

    foreach my $i (0 .. $#_) {
        if ( $_[$i] eq "_price_code" ) {
            my $price = undef;
            given ( $_[$i + 1] ) {
                when($Movie::REGULAR) { $price = Regularprice->new() }
                when($Movie::NEW_RELEASE) { $price = Newreleaseprice->new() }
                when($Movie::CHILDREN) {$price = Childrenprice->new()  }
            }
            push @new_args, '_price';
            push @new_args, $price;
            $skip_next = 1;
        } elsif ( $skip_next == 1 ){
            $skip_next = 0;
        } else {
            push @new_args, $_[$i];
        }
    }

    return $class->$orig(@new_args);
};

sub get_price_code {
    my $self = shift;

    return $self->get_price()->get_price_code();
}

# sub _set_price {
#     my $self = shift;
#     my $price_code = $self->{ _price_code };
#     my $price = undef;

#     given ($price_code) {
#         when($Movie::REGULAR) { $price = Regularprice->new() }
#         when($Movie::NEW_RELEASE) { $price = Newreleaseprice->new() }
#         when($Movie::CHILDREN) {$price = Childrenprice->new()  }
#     }

#     return $price;
# }

sub get_charge {
    my $self = shift;
    my $days_rented = shift;

    return $self->get_price()->get_charge($days_rented);
    # my $days_rented = shift;
    # my $result = 0;
    # given ($self->get_price_code()) {
    #     when($Movie::REGULAR) {
    #         $result = 2;
    #         if ($days_rented > 2) {
    #             $result += ($days_rented - 2) * 1;
    #         }
    #     }
    #     when($Movie::NEW_RELEASE) { $result = $days_rented * 3 }
    #     when($Movie::CHILDREN) {
    #         $result = 1;
    #         if ($days_rented > 3) {
    #             $result += ($days_rented - 3) * 1;
    #         }
    #     }
    # }
    # return $result;
}

sub get_fre_rental_points {
    my $self = shift;
    my $days_rented = shift;
    my $fre_rental_points = 1;

    if ($self->get_price_code() == $Movie::NEW_RELEASE && $days_rented > 1) {
        $fre_rental_points++;
    }

    return $fre_rental_points;
}

1;